package com.avantis.admin.service.impl;

import com.avantis.admin.service.CustomerService;
import com.avantis.common.base.BaseServiceImpl;
import com.avantis.common.entity.Customer;
import com.avantis.common.entity.query.CustomerQuery;
import com.avantis.common.exception.ServiceProcessException;
import com.avantis.common.repository.CustomerRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* @Description:    java类作用描述
* @Author:         kirago
* @CreateDate:     2019/1/14 3:16 PM
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
@Service
@Transactional
public class CustomerServiceImpl extends BaseServiceImpl<CustomerRepository, Customer> implements CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    @Autowired
    private CustomerService customerService;

    @Override
    public boolean checkByCustomerCode(String str) {
        try {
            boolean r = false;
            if (str==null)
            {
                Customer customer = customerRepository.findByCustomerCode(str);

                if (customer==null){
                    r =  false;
                }else {
                    r = true;
                }
            }
            return r;
        }catch (ServiceProcessException e) {
            throw  e;
        }catch (Exception e){
            throw new ServiceProcessException("查找客户编码异常！");
        }
    }


    @Override
    public void saveCustomer(Customer customer){
        try {
            save(customer);
        } catch (ServiceProcessException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceProcessException("新增客户失败！");
        }
    }

    @Override
    public void deleteCustomer(String customerId) {
        customerRepository.deleteByUuid(customerId);
    }

    @Override
    public void modifyCustomer(Customer customer) {
        customerRepository.saveAndFlush(customer);
    }

    @Override
    public Page<Customer> findCustomerCriteria(CustomerQuery customerQuery) {
        Pageable pageable = new PageRequest(customerQuery.getPageNumber()-1,customerQuery.getPageSize());
        Page<Customer> customerPage = customerRepository.findAll(new Specification<Customer>() {

            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(StringUtils.isNotEmpty(customerQuery.getContentName())) {
                    predicates.add(criteriaBuilder.like(root.get("contentName").as(String.class), "%"+customerQuery.getContentName()+"%"));
                }
                if(StringUtils.isNotEmpty(customerQuery.getCustomerCode())) {
                    predicates.add(criteriaBuilder.like(root.get("customerCode").as(String.class), "%"+customerQuery.getCustomerCode()+"%"));
                }
                if(StringUtils.isNotEmpty(customerQuery.getContentPhone())) {
                    predicates.add(criteriaBuilder.like(root.get("contentPhone").as(String.class), "%"+customerQuery.getContentPhone()+"%"));
                }
                if(customerQuery.getCustomerLevel() > 0 ) {
                    predicates.add(criteriaBuilder.equal(root.get("customerLevel").as(String.class), customerQuery.getCustomerLevel()));
                }
                criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
                return criteriaQuery.getRestriction();
            }
        },pageable);

        return customerPage;
    }

    @Override
    public Customer findOne(String customerId) {
        Optional<Customer> customerOptional = repository.findById(customerId);
        if (customerOptional.isPresent()) {
            return customerOptional.get();
        }else {
            return null;
        }
    }
}
